package dolf.game;

import dolf.objects.*;
import dolf.window.XMLHandler;

/*xml foo */

import org.w3c.dom.*;
import javax.xml.xpath.*;


/* xml foo ende */

public class Parcours implements java.io.Serializable {

	private static final long serialVersionUID = 1L;

	
	private String id;
	private String pack;
	private String name;
	private String description;
	private String author;
	private String date;
	private String background;

	private int width;
	private int height;
	private int limit;
	
	
	private Point startpos;
	private java.util.List<WorldObject> objects = new java.util.LinkedList<WorldObject>();
	private java.util.List<GroundObject> grounds = new java.util.LinkedList<GroundObject>();
	private java.util.List<Hole> holes = new java.util.LinkedList<Hole>();
	
	public Parcours(String file) {
		Document doc = XMLHandler.getInstance().getDocument(file, "dolf/game/levels/level.dtd");
		if (doc != null) {
			readXMLFile(doc);
		}
	}
	
	private void readXMLFile(Document doc) {
		XPath xpath = XPathFactory.newInstance().newXPath();
		try {
			id = (String) xpath.evaluate("/level/@id", doc, XPathConstants.STRING);
			pack = (String) xpath.evaluate("/level/@levelpack", doc, XPathConstants.STRING);
			
			name = (String) xpath.evaluate("/level/info/name/text()", doc, XPathConstants.STRING);
			description = (String) xpath.evaluate("/level/info/description/text()", doc, XPathConstants.STRING);
			author = (String) xpath.evaluate("/level/info/author/text()", doc, XPathConstants.STRING);
			date = (String) xpath.evaluate("/level/info/date/text()", doc, XPathConstants.STRING);
			background = (String) xpath.evaluate("/level/info/background/text()", doc, XPathConstants.STRING);
			
			double width1 = (Double) xpath.evaluate("/level/limits/@width", doc, XPathConstants.NUMBER);
			width = (int) width1;
			
			double height1 = (Double) xpath.evaluate("/level/limits/@height", doc, XPathConstants.NUMBER);
			height = (int) height1;
			
			double limit1 = (Double) xpath.evaluate("/level/limit/@lim", doc, XPathConstants.NUMBER);
			limit = (int) limit1;

			double t1 = (Double) xpath.evaluate("/level/startPosition/position/@x", doc, XPathConstants.NUMBER);
			double t2 = (Double) xpath.evaluate("/level/startPosition/position/@y", doc, XPathConstants.NUMBER);
			startpos = new Point(t1, t2); 
			
			NodeList lineObjects = (NodeList) xpath.evaluate("/level/lineObject", doc, XPathConstants.NODESET);
			for (int i = 0; i < lineObjects.getLength(); i++) {
				double x1 = (Double) xpath.evaluate("startPosition/position/@x", lineObjects.item(i), XPathConstants.NUMBER);
				double y1 = (Double) xpath.evaluate("startPosition/position/@y", lineObjects.item(i), XPathConstants.NUMBER);
				
				double x2 = (Double) xpath.evaluate("endPosition/position/@x", lineObjects.item(i), XPathConstants.NUMBER);
				double y2 = (Double) xpath.evaluate("endPosition/position/@y", lineObjects.item(i), XPathConstants.NUMBER);
				
				objects.add(new LineObject(new Point(x1,y1), new Point(x2,y2)));
			}
			
			
			NodeList arcObjects = (NodeList) xpath.evaluate("/level/arcObject", doc, XPathConstants.NODESET);
			for (int i = 0; i < arcObjects.getLength(); i++) {
				double x = (Double) xpath.evaluate("position/@x", arcObjects.item(i), XPathConstants.NUMBER);
				double y = (Double) xpath.evaluate("position/@y", arcObjects.item(i), XPathConstants.NUMBER);

				double tmp = (Double) xpath.evaluate("@radius", arcObjects.item(i), XPathConstants.NUMBER);
				int radius = (int) tmp;
				tmp = (Double) xpath.evaluate("@angle1", arcObjects.item(i), XPathConstants.NUMBER);
				int angle1 = (int) tmp;
				tmp = (Double) xpath.evaluate("@angle2", arcObjects.item(i), XPathConstants.NUMBER);
				int angle2 = (int) tmp;
				objects.add(new ArcObject(new Point(x,y), radius, angle1, angle2));
			}
			
			NodeList pits = (NodeList) xpath.evaluate("/level/pit", doc, XPathConstants.NODESET);
			for (int i = 0; i < pits.getLength(); i++) {
				double x = (Double) xpath.evaluate("position/@x", pits.item(i), XPathConstants.NUMBER);
				double y = (Double) xpath.evaluate("position/@y", pits.item(i), XPathConstants.NUMBER);

				double tmp = (Double) xpath.evaluate("@radius", pits.item(i), XPathConstants.NUMBER);
				int radius = (int) tmp;
				double slope = (Double) xpath.evaluate("@slope", pits.item(i), XPathConstants.NUMBER);
				grounds.add(new Pit(new Point(x,y), radius, slope));
			}
			NodeList sands = (NodeList) xpath.evaluate("/level/sand", doc, XPathConstants.NODESET);
			for (int i = 0; i < sands.getLength(); i++) {
				double x = (Double) xpath.evaluate("position/@x", sands.item(i), XPathConstants.NUMBER);
				double y = (Double) xpath.evaluate("position/@y", sands.item(i), XPathConstants.NUMBER);

				double tmp = (Double) xpath.evaluate("@radius", sands.item(i), XPathConstants.NUMBER);
				int radius = (int) tmp;
				double slope = (Double) xpath.evaluate("@slope", sands.item(i), XPathConstants.NUMBER);
				if(sands.item(i).getAttributes().getNamedItem("slope") != null){
					grounds.add(new Sand(new Point(x,y), radius, slope));
				} else {
					grounds.add(new Sand(new Point(x,y), radius));
				}
			}

			NodeList water = (NodeList) xpath.evaluate("/level/water", doc, XPathConstants.NODESET);
			for (int i = 0; i < water.getLength(); i++) {
				double x = (Double) xpath.evaluate("position/@x", water.item(i), XPathConstants.NUMBER);
				double y = (Double) xpath.evaluate("position/@y", water.item(i), XPathConstants.NUMBER);

				double tmp = (Double) xpath.evaluate("@radius", water.item(i), XPathConstants.NUMBER);
				int radius = (int) tmp;
				grounds.add(new Water(new Point(x,y), radius));
			}
			NodeList hole = (NodeList) xpath.evaluate("/level/hole", doc, XPathConstants.NODESET);
			for (int i = 0; i < hole.getLength(); i++) {
				double x = (Double) xpath.evaluate("position/@x", hole.item(i), XPathConstants.NUMBER);
				double y = (Double) xpath.evaluate("position/@y", hole.item(i), XPathConstants.NUMBER);
				holes.add(new Hole(new Point(x,y)));
			}
		}

		catch (XPathExpressionException e) {
			e.printStackTrace();
		}
	}
	
	public java.util.List<WorldObject> getObjects() {
		return objects;
	}
	public java.util.List<GroundObject> getGrounds() {
		return grounds;
	}
	public java.util.List<Hole> getHoles() {
		return holes;
	}
	public Point getStartPos() {
		return this.startpos;
	}
	public String getID() {
		return this.id;
	}
	public String getLevelpack() {
		return this.pack;
	}
	public String getName() {
		return this.name;
	}
	public String getDescription() {
		return this.description;
	}
	public String getAuthor() {
		return this.author;
	}
	public String getDate() {
		return this.date;
	}
	public String getBackground() {
		return this.background;
	}
	public int getWidth() {
		return this.width;
	}
	public int getHeight() {
		return this.height;
	}
	public int getLimit(){
		return this.limit;
	}
}
